package com.cl.mypureframe.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.cl.mypureframe.R;
import com.cl.mypureframe.conn.Conn;
import com.cl.mypureframe.conn.RetrofitUtil;
import com.cl.mypureframe.minterface.OnResponseCallback;
import com.cl.mypureframe.model.ResLoginMod;
import com.cl.mypureframe.util.Log;
import com.cl.mypureframe.util.UtilToast;
import com.cl.mypureframe.util.Utils;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Response;

/**
 * @Class: LoginActivity
 * @Description: 登录
 * @Author: ChenLei
 * @Date: 2020/11/18---14:52
 */
public class LoginActivity extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContenViewAndBack(R.layout.activity_login, R.string.login);

    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onButtonClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                loginAction();
                break;
        }
    }

    // 登录动作
    public void loginAction() {
        EditText etName = findViewById(R.id.et_name);
        EditText etpwd = findViewById(R.id.et_pwd);
        String name = etName.getText().toString();
        String pwdstr = etpwd.getText().toString();

        // 密码最小长度
        int pwdminLength = getResources().getInteger(R.integer.password_minlength);
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwdstr)) {
            UtilToast.show(R.string.to_notnull);
        } else if (!Utils.checkMobile(name)) {// 手机号合法性
            UtilToast.show(R.string.to_phoneillegal);
        } else if (pwdstr.length() < pwdminLength) {
            UtilToast.show(String.format(getString(R.string.to_notminlength), pwdminLength));
        } else {
            // 执行接口
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("username", name);
            hashMap.put("password", pwdstr);

            RetrofitUtil<ResLoginMod> retrofitUtil = new RetrofitUtil<>();
            Call<ResLoginMod> call = retrofitUtil.getRetrofit().postLogin(hashMap);
            retrofitUtil.connAction(call, this, new OnResponseCallback<ResLoginMod>() {
                @Override
                public void onStartCallback() {
                }

                @SuppressWarnings("ConstantConditions")
                @Override
                public void onSuccessCallback(Response<ResLoginMod> response) {
                    ResLoginMod resLoginMod = response.body();
                    if (resLoginMod.getErrorCode() == Conn.CODE_SUCCESS) {

                    }
                    UtilToast.show(resLoginMod.getErrorMsg());
                }

                @Override
                public void onErrorCallback() {
                }

                @Override
                public void onEndCallback() {
                }
            });

        }
    }
}
